package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.RoleAuthDto;
import com.company.project.model.SysRole;
import com.company.project.service.SysRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by CodeGenerator on 2020/04/18.
*/
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    @PostMapping("/add")
    public Result add(SysRole sysRole) {
        sysRoleService.save(sysRole);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        sysRoleService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(SysRole sysRole) {
        sysRoleService.update(sysRole);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        SysRole sysRole = sysRoleService.findById(id);
        return ResultGenerator.genSuccessResult(sysRole);
    }

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<SysRole> list = sysRoleService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @RequestMapping("/page/manage")
    public ModelAndView manage(){
        ModelAndView modelAndView = new ModelAndView("/workspace/role/roleList.html");
        return modelAndView;
    }

    @GetMapping("/page/detail")
    public ModelAndView detail(){
        ModelAndView modelAndView = new ModelAndView("/workspace/role/roleAdd.html");
        return modelAndView;
    }

    @GetMapping("/page/permission")
    public ModelAndView permission(){
        ModelAndView modelAndView = new ModelAndView("/workspace/role/permission.html");
        return modelAndView;
    }

    /**
     * 修改角色状态
     * @param roleId 角色id
     * @param status 状态
     * @return
     */
    @RequestMapping("/changeStatus")
    public Result changeStatus(@RequestParam int roleId, @RequestParam int status){
        Result Result = sysRoleService.changeStatus(roleId,status);
        return Result;
    }
    /**
     * 停用角色
     * @param roleId 角色id
     * @return
     */
    @RequestMapping("/suspendRole")
    public Result suspendRole(@RequestParam int roleId){
        Result Result = sysRoleService.changeStatus(roleId,2);
        return Result;
    }

    /**
     * 启用角色
     * @param roleId 角色id
     * @return
     */
    @RequestMapping("/resumeRole")
    public Result resumeRole(@RequestParam int roleId){
        Result Result = sysRoleService.changeStatus(roleId,1);
        return Result;
    }

    @RequestMapping("/batchUpdateRoleAuth")
    public Result batchUpdateRoleAuth(@RequestBody RoleAuthDto roleAuthDto){
        Result Result = sysRoleService.batchUpdateRoleAuth(roleAuthDto);
        return Result;
    }


}
